package com.example.a2205_fx.video;

import androidx.lifecycle.MutableLiveData;

import com.example.a2205_fx.base.BaseRepo;
import com.example.a2205_fx.entity.PinglunEntity;
import com.example.a2205_fx.entity.ResultEntity;
import com.example.a2205_fx.entity.VideoEntity;
import com.example.a2205_fx.util.ApiServer;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class VideoRepo extends BaseRepo {
    public void video(int page, int size, MutableLiveData<VideoEntity> success,MutableLiveData<String> fail){
        apiServer.video(page, size)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(VideoEntity entity) {
                        success.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        fail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    public void love(int videoId, MutableLiveData<ResultEntity> loveSuccess,MutableLiveData<String> loveFail){
        apiServer.love(videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResultEntity entity) {
                        loveSuccess.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        loveFail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void nolove(int videoId,MutableLiveData<ResultEntity> noloveSuccess,MutableLiveData<String> noloveFail){
        apiServer.nolove(videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResultEntity entity) {
                        noloveSuccess.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        noloveFail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getComment(int videoId, MutableLiveData<PinglunEntity> getSuccess,MutableLiveData<String> getFail){
        apiServer.getComment(videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PinglunEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(PinglunEntity entity) {
                        getSuccess.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        getFail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void comment(RequestBody body,MutableLiveData<PinglunEntity> comSucc,MutableLiveData<String> comFail){
        apiServer.comment(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PinglunEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(PinglunEntity entity) {
                        comSucc.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        comFail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
